package com.aspire.moss.portal.desktop;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.aspire.moss.portal.R;

/**
 * 整个桌面的总布局
 * 
 * @author Administrator
 * 
 */
public class DragLayer extends FrameLayout implements MoveIconController {

	private static final String TAG = "DragLayer";
	
	private Context mContext;

	Bitmap wallpaper;

	private ImageView moveIconView;
	
	private AppInfo moveAppInfo;//保存手指移动的应用的信息

	private Bitmap moveIconBitmap, longClickBitmap; //移动图标图像 , 长按图标图像

	private int moveIconX, moveIconY , moveIconW, moveIconH;//移动图标当前的X ,Y ,宽 ,高
	
	private FrameLayout.LayoutParams moveIconparams;

	private onInterceptTouchEvent interceptTouchEvent;

	public DragLayer(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public DragLayer(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.DragLayer, defStyle, 0);
		Drawable drawable = a.getDrawable(R.styleable.DragLayer_wallpaper);
		wallpaper = null == drawable ? null : ((BitmapDrawable) drawable)
				.getBitmap();
	}

	public void initMoveIcon(AppView longClickView) {
		this.moveAppInfo = ((AppView)longClickView).appInfo;
		final int longClickLeft = longClickView.getLeft();
		final int longClickTop = longClickView.getTop();
		longClickBitmap = Utils.getViewBitmap(longClickView);//得到longClickView的缩略图
		moveIconBitmap = Utils.scaleBitmap(longClickBitmap, Config.ZOOMCELLVIEWICON);//把longClickView的缩略图放大下,给moveIconBitmap
		moveIconW = moveIconBitmap.getWidth();
		moveIconH = moveIconBitmap.getHeight();
		//计算moveIconView的位置
		final int moveIconLeft = longClickLeft - (moveIconW - longClickBitmap.getWidth()) / 2 ;
		final int moveIconTop = longClickTop - (moveIconH - longClickBitmap.getHeight()) / 2;
		moveIconView = new ImageView(mContext);
		moveIconView.setImageBitmap(moveIconBitmap);
		moveIconparams = new FrameLayout.LayoutParams(moveIconW, moveIconH);
		moveIconparams.gravity = Gravity.TOP;
		moveIconparams.setMargins(moveIconLeft, moveIconTop, moveIconLeft + moveIconW, moveIconTop + moveIconH);
		moveIconView.setLayoutParams(moveIconparams);
		this.addView(moveIconView, moveIconparams);
		moveIconView.bringToFront();
		moveIconView.requestFocus();
		longClickView.setVisibility(View.GONE);
	}

	public void moveIcon(float rawX, float rawY) {
		moveIconX = (int) (rawX - moveIconView.getWidth() / 2);
		moveIconY = (int) (rawY - moveIconView.getHeight() / 2);
		moveIconparams.setMargins(moveIconX, moveIconY, Utils.getScreenWidth(mContext)
				- moveIconX - moveIconW, Utils.getScreenHeight(mContext)
				- moveIconY - moveIconH);
		moveIconView.setLayoutParams(moveIconparams);
		moveIconView.requestFocus();
	}

	@Override
	public void resetMoveIcon(float pointX, float pointY , final OnMoveIconResetCallBack callBack ,
			final AppInfo appInfo , final AppInfo target , final AppViewAction action) {
		final float targetX = pointX - moveIconX;
		final float targetY = pointY - moveIconY;
		moveIconView.setImageBitmap(longClickBitmap);//把图标放小
		Animation transfer = new TranslateAnimation(0.0f ,targetX ,0.0f , targetY);
		transfer.setDuration(Config.MOVE_ICON_RESET_TIME);
		transfer.setAnimationListener(new ViewAnimationListener(){
			@Override
			public void onAnimationEnd(Animation animation) {
				super.onAnimationEnd(animation);
				removeView(moveIconView);
				moveIconX = moveIconY = 0;
				if(null != callBack) callBack.onResetEnd(appInfo , target , action);
			}
		});
//		transfer.setFillEnabled(true);
		moveIconView.startAnimation(transfer);
		moveIconView.bringToFront();
		moveIconView.requestFocus();
	}
	
	@Override
	public int getMoveIconX() {
		return moveIconX;
	}

	@Override
	public int getMoveIconY() {
		return moveIconY;
	}

	@Override
	public int getMoveIconWidth() {
		return moveIconW;
	}
	
	@Override
	public int getMoveIconHeight() {
		return moveIconH;
	}

	/**
	 * 是否拦截该事件,当用记长按,拖动应用时,应该拦截
	 */
	@Override
	public boolean onInterceptTouchEvent(MotionEvent event) {
		Log.i(TAG, "onInterceptTouchEvent  event=" + event);
		if (EventCenter.isLongClick) {
			 interceptTouchEvent.onDragLayeronIntercept(event);
			 return true;
		}
		return super.onInterceptTouchEvent(event);
	}

	/**
	 * 是否自己处理请该事件,当用户长按时,自己处理,调用onTouchEvent实例处理
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		Log.i(TAG, "onTouchEvent  event=" + event);
		if (EventCenter.isLongClick) {
			return interceptTouchEvent.onDragLayeronIntercept(event);
		}
		return super.onTouchEvent(event);
	}

	@Override
	public AppInfo getAppInfo() {
		return this.moveAppInfo;
	}

	@Override
	public void setAppInfo(AppInfo appInfo) {
		this.moveAppInfo = appInfo;
	}
	
	public void setInterceptTouchEvent(onInterceptTouchEvent interceptTouchEvent) {
		this.interceptTouchEvent = interceptTouchEvent;
	}

	/**
	 * 处理屏幕框架的事件
	 * 
	 * @author Administrator
	 * 
	 */
	public interface onInterceptTouchEvent {

		/**
		 * 处理拖动图标事件
		 * 
		 * @param event
		 * @return
		 */
		public boolean onDragLayeronIntercept(MotionEvent event);

	}
	
	/**
	 * 当移动图标动画完成时
	 * @author Administrator
	 *
	 */
	interface OnMoveIconResetCallBack{
		
		/**
		 * 当移动图标重置完成,回调,这个时 应该真正的移动你选中的
		 * @param appInfo
		 * @param target
		 * @param action
		 */
		void onResetEnd(AppInfo appInfo , AppInfo target , AppViewAction action);
		
	}

}



